package com.wangzhen.spring.inject;

import com.wangzhen.spring.inject.annotation.MyAutowired;
import com.wangzhen.spring.ioc.dependcy.entity.User;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Collection;
import java.util.Optional;

/**
 * 通过扩展@Autowired注解，演示是否能够将字段注入到bean对象中
 */
public class MyAutowiredInjectDemo {

    // xml文件中配置的User，primary=true ---->实时注入
    @MyAutowired
    private User user;

    @Autowired
    private Optional<String> hello;

     public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(MyAutowiredInjectDemo.class);
        // 使用注解方式驱动的AnnotationConfigApplicationContext，也可以加载xml配置
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(applicationContext);
        reader.loadBeanDefinitions("classpath:/META-INF/dependcy-lookup-context.xml");
        // 启动应用上下文
        applicationContext.refresh();
        // 获取UserHolder
        MyAutowiredInjectDemo demo = applicationContext.getBean(MyAutowiredInjectDemo.class);
        System.out.println("demo.user ="+demo.user);

         System.out.println(applicationContext.getBean("hello"));
        // 关闭应用上下文
        applicationContext.close();
    }

}
